﻿using System;
using System.Configuration;

namespace SPDeployer.Core.Config
{
    public class FeatureConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty(Definitions.Features.ACTIVATE, IsRequired = false, DefaultValue = false)]
        public bool Activate
        {
            get { return Boolean.Parse(this[Definitions.Features.ACTIVATE].ToString()); }
            set { this[Definitions.Features.ACTIVATE] = value; }
        }

        [ConfigurationProperty(Definitions.Lists.TYPE, IsRequired = false, DefaultValue = 101)]
        public Guid Id
        {
            get { return new Guid(this[Definitions.Features.ID].ToString()); }
            set { this[Definitions.Features.ID] = value; }
        }

        [ConfigurationProperty(Definitions.Features.NAME, IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return this[Definitions.Features.NAME].ToString(); }
            set { this[Definitions.Features.NAME] = value; }
        }
    }

    [ConfigurationCollection(typeof(FeatureConfigurationElement),
        AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
    public class FeatureConfigurationElementCollection : ConfigurationElementCollection
    {
        public FeatureConfigurationElement this[int index]
        {
            get { return BaseGet(index) as FeatureConfigurationElement; }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new FeatureConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FeatureConfigurationElement)element).Id;
        }
    }
}